﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DiemChapNhanThe.Entities;
using DiemChapNhanThe.Data;

public partial class Admin_MatHang_CNMatHang : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        imgViewer.ImageUrl = H_AnhDaiDien.Value.Length > 0 ? Common.sPathUpMatHang + H_AnhDaiDien.Value : "~/Images/no_image.jpg";
        if (!Page.IsPostBack)
        {
            BindCatalog.BindLoaiCongTy(cboLoaiCongTy);
            BindCatalog.BindLoaiMatHang(cboLoaiMatHang);
            BindCatalog.BindSubMatHang(cboSubMatHang);
            BindCatalog.BindDiaPhuong(cboDiaPhuong);
            if (Request.QueryString["id"] != null)
            {
                H_CatalogId.Value = Request.QueryString["id"].ToString();
                BindData(Convert.ToInt32(H_CatalogId.Value));
            }

            TList<SubMatHang> TList_Catalogs = DataRepository.SubMatHangProvider.Find("IdLoaiMatHang=" + cboLoaiMatHang.SelectedValue);
            cboSubMatHang.DataSource = TList_Catalogs;
            cboSubMatHang.DataTextField = "TenSubMatHang";
            cboSubMatHang.DataValueField = "Id";
            cboSubMatHang.DataBind();
        }
    }
    protected void BindData(int IdCategories)
    {
        using (MatHang ent_MatHang = DataRepository.MatHangProvider.GetById(IdCategories))
        {
            using (SubMatHang ent_SubMatHang = DataRepository.SubMatHangProvider.GetById(ent_MatHang.IdSubMatHang))
            {
                using (LoaiMatHang ent_LoaiMatHang = DataRepository.LoaiMatHangProvider.GetById(ent_SubMatHang.IdLoaiMatHang))
                {
                    H_CatalogId.Value = ent_MatHang.Id.ToString();
                    cboLoaiMatHang.SelectedValue = ent_LoaiMatHang.Id.ToString();
                }
            }
            //entTour.LuotXem = Convert.ToInt32(H_LuotXem.Value);
            cboSubMatHang.SelectedValue = ent_MatHang.IdSubMatHang.ToString();
            cboDiaPhuong.SelectedValue = ent_MatHang.IdDiaPhuong.ToString();
            cboLoaiCongTy.SelectedValue = ent_MatHang.IdLoaiCongTy.ToString();
            txtTieuDe.Text = ent_MatHang.TieuDe;
            H_AnhDaiDien.Value = ent_MatHang.AnhDaiDien;
            imgViewer.ImageUrl = ent_MatHang.AnhDaiDien.Length > 0 ? Common.sPathUpMatHang + ent_MatHang.AnhDaiDien : "no_image.jpg";
            txtNgayBatDau.Text = ent_MatHang.NgayBatDau.ToString("dd/MM/yyyy");
            txtNgayKetThuc.Text = ent_MatHang.NgayKetThuc.ToString("dd/MM/yyyy");
            txtPhanTram.Text = ent_MatHang.PhanTram.ToString();
            txtDiemNoiBat.Text = Server.HtmlDecode(ent_MatHang.DiemNoiBat);
            txtDieuKienSuDung.Text = Server.HtmlDecode(ent_MatHang.DieuKienSuDung);
            txtTomTat.Text = ent_MatHang.TomTat;
            txtNoiDung.Text = Server.HtmlDecode(ent_MatHang.NoiDung);
        }

    }

    protected void cmdLuu_Click(object sender, ImageClickEventArgs e)
    {
        bool updateStatus = false;
        try
        {
            using (MatHang ent_MatHang = new MatHang() { IdDiaPhuong = Convert.ToInt32(cboDiaPhuong.SelectedValue), IdLoaiCongTy = Convert.ToInt32(cboLoaiCongTy.SelectedValue), IdLoaiMatHang = Convert.ToInt32(cboLoaiMatHang.SelectedValue) ,IdSubMatHang = Convert.ToInt32(cboSubMatHang.SelectedValue), TieuDe = Common.StandardString(txtTieuDe.Text), NgayBatDau = DateTime.ParseExact(Common.StandardString(txtNgayBatDau.Text), "dd/MM/yyyy", null), NgayKetThuc = DateTime.ParseExact(Common.StandardString(txtNgayKetThuc.Text), "dd/MM/yyyy", null), PhanTram = Convert.ToInt32(txtPhanTram.Text), DiemNoiBat = Server.HtmlDecode(txtDiemNoiBat.Text), DieuKienSuDung = Server.HtmlDecode(txtDieuKienSuDung.Text), TomTat = Common.StandardString(txtTomTat.Text), NoiDung = Server.HtmlEncode(txtNoiDung.Text) })
            {
                DateTime NgayBD = DateTime.ParseExact(Common.StandardString(txtNgayBatDau.Text), "dd/MM/yyyy", null);
                ent_MatHang.NgayBatDau = DateTime.ParseExact(Common.StandardString(txtNgayBatDau.Text), "dd/MM/yyyy", null);
                if (txtNgayKetThuc.Text != "")
                {
                    DateTime NgayKT = DateTime.ParseExact(Common.StandardString(txtNgayKetThuc.Text), "dd/MM/yyyy", null);
                    if (NgayBD > NgayKT)
                    {
                        ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "Thất bại", "alert('Ngày bắt đầu không thể lớn hơn ngày kết thúc!');", true);
                        return;
                    }
                    else
                    {
                        ent_MatHang.NgayKetThuc = DateTime.ParseExact(Common.StandardString(txtNgayKetThuc.Text), "dd/MM/yyyy", null);
                    }
                }
                if (H_AnhDaiDien.Value.Length == 0)
                {
                    ent_MatHang.AnhDaiDien = "no_image.jpg";
                }
                else
                {
                    ent_MatHang.AnhDaiDien = Common.StandardString(H_AnhDaiDien.Value);
                }
                ent_MatHang.TrangThai = chkTrangThai.Checked;
                ent_MatHang.NgayCapNhat = Common.GetServerDateTime();
                if (H_CatalogId.Value == "")
                {
                    updateStatus = DataRepository.MatHangProvider.Insert(ent_MatHang);
                }
                else
                {
                    ent_MatHang.Id = Convert.ToInt32(H_CatalogId.Value);
                    ent_MatHang.EntityState = EntityState.Changed;
                    updateStatus = DataRepository.MatHangProvider.Update(ent_MatHang);
                }
                if (!updateStatus)
                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.MATHANG_CAPNHAT_THATBAI, true);
                else
                {
                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THANHCONG, Constants.MATHANG_CAPNHAT_THANHCONG, true);
                    RedirectTo("~/Admin/MatHang/QLMatHang.aspx");
                }
            }

        }

        catch (Exception ex)
        {
            Common.WriteLog("CNMatHang.aspx.cs", "cmdLuu_Click", ex.ToString());
            ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.MATHANG_CAPNHAT_THATBAI, true);
        }
    }

    private void RedirectTo(string url)
    {
        string redirectURL = Page.ResolveClientUrl(url);
        string script = String.Format("window.location = '{0}';", redirectURL);
        ScriptManager.RegisterStartupScript(this, typeof(Page), "RedirectTo", script, true);
    }

    protected void cmdNhapTiep_Click(object sender, ImageClickEventArgs e)
    {
        H_CatalogId.Value = "";
        H_AnhDaiDien.Value = "no_image.jpg";
        txtTieuDe.Text = "";
        txtTomTat.Text = "";
        chkTrangThai.Checked = true;
        txtNgayBatDau.Text = "";
        txtNgayKetThuc.Text = "";
        txtPhanTram.Text = "";
        txtDiemNoiBat.Text = "";
        txtDieuKienSuDung.Text = "";
        txtNoiDung.Text = "";
        BindCatalog.BindLoaiMatHang(cboLoaiMatHang);
        BindCatalog.BindSubMatHang(cboSubMatHang);
        BindCatalog.BindDiaPhuong(cboDiaPhuong);
        BindCatalog.BindLoaiCongTy(cboLoaiCongTy);
    }
    protected void SelectedindexchangedcboLoaiMatHang(object sender, EventArgs e)
    {
        TList<SubMatHang> TList_SubMatHang = DataRepository.SubMatHangProvider.Find("IdLoaiMatHang=" + cboLoaiMatHang.SelectedValue);
        cboSubMatHang.DataSource = TList_SubMatHang;
        cboSubMatHang.DataTextField = "TenSubMatHang";
        cboSubMatHang.DataValueField = "Id";
        cboSubMatHang.DataBind();
    }
}